﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Xml;

public partial class MasterPageEmployee : System.Web.UI.MasterPage
{
    EmployeeMethord EM = new EmployeeMethord();
    protected void Page_Load(object sender, EventArgs e)
    {
        lblEmail.Visible = false;
        Logindiv();
        LeftMenuVacancy.Text = LoadLeftMenuVacancy();
        LeftMenuInterview.Text = LoadLeftMenuInterview();
        LeftMenuApplicant.Text = LoadLeftMenuApplicant();
        LoadMessage();
    }
    public void Logindiv()
    {
        try
        {
            if ((Session["User"].ToString() != "") || (Session["User"] != null))
            {
                lblWel.Text = "Welcome:";
                lblUserName.Text = Session["User"].ToString();
                btnLogout.Text = "Logout";
            }
            else
            {
                lblWel.Text = "";
                lblUserName.Text = "";
                btnLogout.Text = "Login";
                Response.Redirect("~/LoginMaster.aspx");
            }
        }
        catch (Exception ex)
        {
            lblWel.Text = "";
            lblUserName.Text = "";
            btnLogout.Text = "Login";
            Response.Redirect("~/LoginMaster.aspx");
        }
    }
    private string LoadLeftMenuApplicant()
    {
        string LeftMenu = "";
        try
        {
            DataTable tb = EM.MenuTypeJob();
            if (tb.Rows.Count > 0)
            {
                foreach (DataRow item in tb.Rows)
                {
                    LeftMenu += "<dt><a class=\"\" href=\"\"><div class=\"listmanager\">";
                    LeftMenu += item["JobName"].ToString();
                    LeftMenu += "</div></a></dt>";
                    DataTable tb_Vacancy = EM.MenuVacancyEmployee(Convert.ToInt32(item["JobID"].ToString()), Session["User"].ToString());
                    if (tb_Vacancy.Rows.Count > 0)
                    {
                        LeftMenu += "<dd><ul>";
                        foreach (DataRow itemA in tb_Vacancy.Rows)
                        {
                            LeftMenu += "<li><a href=\"HRApplicantApplyVacancy.aspx?ID=" + itemA["VacancyID"].ToString() + "\">" + itemA["Title"].ToString() + "</a></li>";
                        }
                        LeftMenu += "</ul></dd>";
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
        return LeftMenu;
    }
    private string LoadLeftMenuInterview()
    {
        string LeftMenu = "";
        try
        {
            DataTable tb =  EM.MenuTypeJob();
            if (tb.Rows.Count > 0)
            {
                foreach (DataRow item in tb.Rows)
                {
                    LeftMenu += "<dt><a class=\"\" href=\"\"><div class=\"listmanager\">";
                    LeftMenu += item["JobName"].ToString();
                    LeftMenu += "</div></a></dt>";
                    DataTable tb_Vacancy = EM.MenuVacancyEmployee(Convert.ToInt32(item["JobID"].ToString()), Session["User"].ToString());
                    if (tb_Vacancy.Rows.Count > 0)
                    {
                        LeftMenu += "<dd><ul>";
                        foreach (DataRow itemI in tb_Vacancy.Rows)
                        {
                            LeftMenu += "<li><a href=\"HRViewIntervewInformation.aspx?ID=" + itemI["VacancyID"].ToString() + "\">" + itemI["Title"].ToString() + "</a></li>";
                        }
                        LeftMenu += "</ul></dd>";
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
        return LeftMenu;
    }
    private string LoadLeftMenuVacancy()
    {
        string LeftMenu = "";
        try
        {
            DataTable tb =  EM.MenuTypeJob();
            if (tb.Rows.Count > 0)
            {
                foreach (DataRow item in tb.Rows)
                {
                    LeftMenu += "<dt><a class=\"active\" href=\"\"><div class=\"listmanager\">";
                    LeftMenu += item["JobName"].ToString();
                    LeftMenu += "</div></a></dt>";
                    DataTable tb_Vacancy = EM.MenuVacancyEmployee(Convert.ToInt32(item["JobID"].ToString()), Session["User"].ToString());
                    if (tb_Vacancy.Rows.Count > 0)
                    {
                        LeftMenu += "<dd><ul>";
                        foreach (DataRow itemV in tb_Vacancy.Rows)
                        {
                            LeftMenu += "<li><a href=\"HRViewVacancyDetails.aspx?ID=" + itemV["VacancyID"].ToString() + "\">" + itemV["Title"].ToString() + "</a></li>";
                        }
                        LeftMenu += "</ul></dd>";
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
        return LeftMenu;
    }
    protected void btnLogout_Click(object sender, EventArgs e)
    {
        if (btnLogout.Text == "Logout")
        {
            lblUserName.Text = "";
            lblWel.Text = "";
            Session.Abandon();
            Response.Cookies.Clear();
            btnLogout.Text = "Login";
            Response.Redirect("../LoginMaster.aspx");
        }
        else
        {
            Response.Redirect("../LoginMaster.aspx");
        }
    }
    protected void Page_Init(object sender, EventArgs e)
    {

        Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.Cache.SetNoStore();
    }

    public void LoadMessage()
    {
        try
        {
            SqlDataReader dr = EM.ReturnInterviewNotAppcess(Session["User"].ToString());
            while (dr.Read())
            {
                using (XmlTextReader reader = new XmlTextReader(Server.MapPath("~/ProposedInterview/"+Session["User"].ToString()+"_"+dr.GetInt32(0)+".xml")))
                {
                    while (reader.Read())
                    {
                        lblEmail.Visible = true;
                        break;
                    }
                }
            }
        }
        catch (Exception ex)
        {
        }
    }
}
